/*Author: Rony Lahav (ID:043489889)*/

import java.util.Collection;
import java.util.TreeMap;


public class StatusManagerReg extends StatusManager implements Runnable{

	public void run(){
		do{
			if (!final_project.isShutDown() && !final_project.inFallBackMode) {
				long currentTime=System.currentTimeMillis();
				DBTuple tuple = final_project.getMyMainSN();
				if (tuple!=null && (tuple.getLastConnectionTime()+final_project.timeout)<=currentTime){	//peer needs to become offline
					tuple.setStatus("0");	//SN offline in reg mode
					tuple.setIsMainSN(false);
					DataBase.getInstance().updateExistingTuple(tuple);
					//find alternative SN or go to FallBack mode
					long id = final_project.getNewUniqueID();
					MainSNLocator locateMainSN = new MainSNLocator(id);
					Thread thread = new Thread(locateMainSN);
					final_project.addThreadToLivingList(id, thread);
					thread.start();
				}
			}
			try{
				// Sleep until next update frequency
				Thread.sleep(final_project.frequency);
			}
			catch (InterruptedException e){}
		} while (!final_project.isShutDown() && !final_project.inFallBackMode);
	}
}
